﻿namespace UsefulTools.Wpf
{
    #region Usings

    using System;
    using System.Windows;
    using System.Windows.Forms;
    using System.Windows.Interop;
    using System.Windows.Media;
    using UsefulTools.Core;
    using UsefulTools.ExtensionMethods;
    using IWin32Window = System.Windows.Forms.IWin32Window;

    #endregion

    public class MessageService : IMessageService
    {
        private readonly IOwnerWindowProvider _provider;

        public MessageService(
            IOwnerWindowProvider provider)
        {
            _provider = provider;
        }
        public static IWin32Window GetIWin32Window(Visual visual)
        {
            HwndSource source = PresentationSource.FromVisual(visual).CastTo<HwndSource>();
           IWin32Window win = new OldWindow(source.Handle);
            return win;
        }

        private class OldWindow : IWin32Window
        {
            private readonly IntPtr _handle;
            public OldWindow(IntPtr handle)
            {
                _handle = handle;
            }


            IntPtr IWin32Window.Handle
            {
                get
                {
                    return _handle;
                }
            }

        }

        private IWin32Window GetWindow(IWindow owner)
        {
            return (IWin32Window) (owner == null || (owner as Visual) == null ? 
                _provider.GetWindow() : GetIWin32Window((Visual)owner));
        }


        public void ShowMessage(IWindow owner, string message)
        {
            System.Windows.Forms.MessageBox.Show(
                   GetWindow(owner),
                   message,
                   TitlePart() + "Information",
                   MessageBoxButtons.OK,
                   MessageBoxIcon.Information);

        }


  
        public void ShowWarning(IWindow owner, string message)
        {
            System.Windows.Forms.MessageBox.Show(
                   GetWindow(owner),
                   message,
                   TitlePart() + "Warning",
                   MessageBoxButtons.OK,
                   MessageBoxIcon.Exclamation);
        }

        private string TitlePart()
        {
            return _provider.GetWindowTitle() + " - ";
        }

        public void ShowFatalError(IWindow owner, string message)
        {

            System.Windows.Forms.MessageBox.Show(
                              GetWindow(owner),
                              message,
                              TitlePart()+"Unhandled Exception",
                              MessageBoxButtons.OK,
                              MessageBoxIcon.Error);

        }
        public void ShowError(IWindow owner, string message)
        {

            System.Windows.Forms.MessageBox.Show(
                              GetWindow(owner),
                              message,
                              TitlePart() + "Error",
                              MessageBoxButtons.OK,
                              MessageBoxIcon.Error);
        }
   
        public bool ShowYesNoQuestion(IWindow owner, string message)
        {
            DialogResult result = System.Windows.Forms.MessageBox.Show(
                              GetWindow(owner),
                              message,
                              TitlePart() + "Error",
                              MessageBoxButtons.YesNo,
                              MessageBoxIcon.Question,
                              MessageBoxDefaultButton.Button2);

            return result == DialogResult.Yes;

           
        }

        
    }


}